//
// Created by 11510 on 2024/7/29.
//

#ifndef WEB_DEDUPLICATION_WEBPAGE_H
#define WEB_DEDUPLICATION_WEBPAGE_H

#include "simhash/Simhasher.hpp"
#include "SplitTool.h"
#include <string>
#include <vector>
#include <map>
#include <algorithm>

class WebPage {
private:
    const static int TOPK_NUMBER = 20;
    uint64_t uint64;

    int docId;/* 文章id */

    std::string docTitle;/* 文章标题 */
    std::string docUrl;/* 文章url */
    std::string docContent;/* 文章内容 */

    /*文章的所有词语对应的词频，不包含停用词*/
    std::map<std::string,int> wordsMap;

    /*文章的所有词语对应的权重*/
    std::map<std::string,double> wordWeightMap;

public:
    // 构造函数，使用移动构造
    WebPage(int id, std::string &&title, std::string &&url,
            std::string &&content) :
            docId(id),
            docTitle(std::move(title)),
            docUrl(std::move(url)),
            docContent(std::move(content)) {

    }


    const int getDocId() const { return docId; }
    const std::string &getDocTitle() const {
        return docTitle;
    }
    const std::string &getDocUrl() const {
        return docUrl;
    }
    const std::string &getDocContent() const {
        return docContent;
    }
    const std::map<std::string, int> &getWordsMap()const{
        return wordsMap;
    }

    std::map<std::string, double> &getWordWeightMap(){
        return wordWeightMap;
    }
//    计算所有词语和词频

    /**
     * 分割文章内的词语并统计词频
     * @param tool
     */
    void splitWordAndCntFrequency(SplitTool *tool){
        std::vector<std::string> words;
        tool->cutWords(docContent, words);

        std::for_each(words.begin(), words.end(),[this](const std::string& s){
            wordsMap[s]++;
        });
    }

    /**
     * 计算哈希值
     * @param simhasher
     */
    void hashCodeOfContent(Simhasher *simhasher) {
        simhasher->make(docContent, TOPK_NUMBER, uint64);
    }

    /**
     * 两篇文章判等
     * @param lhs
     * @param rhs
     * @return
     */
    friend bool operator==(const WebPage &lhs, const WebPage &rhs);

//    对文档按Docid进行排序
    friend bool operator<(const WebPage &lhs, const WebPage &rhs);
};

bool operator==(const WebPage &lhs, const WebPage &rhs) {
    return simhash::Simhasher::isEqual(lhs.uint64, rhs.uint64);
}


#endif //WEB_DEDUPLICATION_WEBPAGE_H
